/************************************************************************
*
* Purpose: WartzMUD video game
* Author:  J G Schlimmer
* Date:    13-Jun-09
* Last updated: 15-Jun-09
*
************************************************************************/
//////////////////////////////  TEMP INFO  //////////////////////////////
//                   READ THIS - READ THIS - READ THIS
//       CHECK TOP COMMENT IN void proctrade( int items1, int items2 )
//       DID NOT HAVE ENOUGH TIME TO GET A WORKING COMPILATIOB AT WORK
/////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>

#define EXCHANGERATE 1.66
#define LOWTEMP -40
#define HIGHTEMP 130
#define BASEHP 100

enum RACES_T 
{
	ELF, TROLL, HUMAN, UNDEAD
};
enum MONTHS_T 
{
	JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,NOV,DEC
};
enum DAYS_T 
{
	SUN,MON,TUE,WED,THR,FRI,SAT
};
enum COMMANDS_T 
{
	EXIT,VIEW,OPEN,CLOSE,SAVE,NEW,HELP,MAN
};
enum RIFLES_T 
{
	MAUSER,M107,GARAND,MOSIN,M16,AK47
};
enum MELEE_EDGED_T 
{
	TWOHANDER,DAGGER,ONEHANDER,SCIMITAR
};
enum MELEE_BLUNT_T 
{
	WOODCLUB,IRONCLUB,MACE,STAFF
};

void write_logs(void);
void read_logs(void);
void commands_list(void);
void manual(void);
void console_gfx(void);
void console_header(void);
void run_command(char command_enter[]);
void listraces();
void attack();
void readRoom();
void manipObject();
void createChar();
void gameLoop();
int readCharStats();
void fetchHealth(int *h);
void inittrade();
void proctrade(int items1, int items2);
int selectjunktotrade(int junk1, int junk2);

/************************************************************************/
char command_enter[100];
FILE *fp;
static const char *DUNGEONS = "dungeons.txt";
static const char *LOGFILE = "logs.txt";
static const char *CHARACTER = "char.txt";
static const char *GAMESTATS = "gamestats.txt";
static const char *INVENTORY = "inventory.txt";
const size_t SIZE = 32;

/************************************************************************/
int main(int argc, char *argv[])
{
	console_header();
	console_gfx();
	return 0;
}

/************************************************************************/
void write_logs(void)
{
	errno_t err;
	int numclosed;

	if((err  = fopen_s(&fp, LOGFILE, "a" )) !=0)
	{
		printf("'logs.txt' does not exist?\n");
	}
	else
	{
		fprintf_s(fp, "bob\n");
	}
	if(fp)
	{
		if (fclose(fp))
		{
			printf("The file 'logs.txt' was not closed\n");
		}
	}
	numclosed = _fcloseall();
	printf("Number of files closed by _fcloseall: %u\n", numclosed);

}

/************************************************************************/
void console_gfx(void)
{
	printf(">>> ");
	scanf_s("%s", &command_enter, 100);
	if(strlen(command_enter) == 0)
	{
		printf("No Command entered\n");
		console_gfx();
	}
	else
	{
		run_command(command_enter);
	}
}

/************************************************************************/
void console_header(void)
{
	printf("WartzMUD .01 \n_______________\nSee the manual(man)  \nfor information about \ncommands.\n\n");
}

/************************************************************************/
void read_logs(void) 
{
	errno_t err = fopen_s(&fp, LOGFILE, "r");
	char listFile[25];
	if(err)
	{
		printf("Error opening file\n");
	}
	else 
	{
		printf("file contents\n--------------\n");
		fscanf_s(fp, "%s", listFile, 25);
		printf("%s\n", listFile);
		fclose(fp);
	}
}

/************************************************************************/
/*
/*Lists all valid commands and a short description
*/
void commands_list(void)
{
	printf(":::::::::::Commands::::::::::::\n");
	printf(":: view :: View list of all Logs.\n");
	printf(":: open :: Opens a log.\n");
	printf(":: exit :: Quits the program.\n");
	printf(":: close :: Closes a log.\n");
	printf(":: save :: Saves a log.\n");
	printf(":: new :: Creates a new log.\n");
	printf(":: help :: Shows this command list.\n");
	printf(":: man :: Shows the manual.\n");
}

/************************************************************************/
/*
/*manual() should take the manaual from a html or txt file and load it/parse it.*/
/*Not sure what yet
*/
void manual(void)
{
	puts("The manual");
	puts("type \"help\" to see a complete list of commands.");
}

/************************************************************************/
/*
/*run_command takes the command_enter variable as parem and checks through it to compare it to the below system commands
/*[exit] (y, n) exits the program after getting confirmation from user.
/*[view] logs?? saves?? characters?? havent fully implemented this yet.
/*[open] calls readRoom() to display a list of dungeons to explore from a txt file. Not fully implemented.
/*[close] should either close a running game or close (clear) whatever you are doing atm and just display a clean console_header()/_gfx(). Not implemented.
/*[save] calls write_logs() which currently writes a string to a txtfile log. Should eventually save your character's status in the gameworld.
/*[new] calls createChar() to create a new character. Currently asks for the character's name, race and sex and saves that to a single txt file. Should save to individual txt files.
/*[help] calls commands_list() to list these commands on the console with a short description for each.
/*[man] calls manual() to display the full manual.
/*[attack] calls attack() to start a battle. Will eventually take runtime parameters ("attack _name_ using _weapon_"). Hardly implemented at all.
/*else print "Not a command"
*/
void run_command(char command_enter[])
{
	char y_n[2];
	printf("You ran this command: %s\n", command_enter);
	if (strcmp(command_enter, "exit") == 0)						//Test command_enter from console_gfx() for command to exiting the program
	{															//I really need to figure out a less clumsy way of doing this
		printf( "are you sure you want to exit? (y/n)\n" );
		printf(">>> ");
		scanf_s("%s", &y_n, 2);
		if (strcmp(y_n, "y") == 0)								//strcmp checking user input to y_n to see if it == [y]es.
		{
			exit(1);
		}
		else if (strcmp(y_n, "n") == 0)
		{
			printf("canceled...\n");
		}
		else
		{
			printf("Not a command\n");
		}
	}
	else if (strcmp(command_enter, "view") == 0)
	{
		printf( "__LIST OF LOGS__\n" );
	}
	else if (strcmp(command_enter, "open") == 0)
	{
		printf( "Opening List of Dungeons\n" );
		readRoom();
	}
	else if (strcmp(command_enter, "close") == 0)
	{
		printf( "Closing...\n" );
	}
	else if (strcmp(command_enter, "save") == 0)
	{
		printf( "Saveing...\n" );
		write_logs();
	}
	else if (strcmp(command_enter, "new") == 0)
	{
		printf( "New Character\n" );
		createChar();
	}
	else if (strcmp(command_enter, "help") == 0)
	{
		commands_list();
	}
	else if (strcmp(command_enter, "man") == 0)
	{
		manual();
	}
	else if (strcmp(command_enter, "attack") == 0)
	{
		attack();
	}
	else if( strcmp( command_enter, "play" ) == 0 )
	{
		gameLoop();
	}
	else
	{
		printf("Not a command\n");
	}
	console_gfx();
}
/************************************************************************/
void listraces()
{
	int race=0;
	int racepop=0;
	scanf_s("%c", race);
	printf("%c\n", race);
	if (race==TROLL)
	{
		racepop+=1;
		printf("grunt!\n");
	}
}

/************************************************************************/
void attack()
{
	int damage=0;
	char name[25];
	damage=readCharStats();
	printf("You attacked %c for %d damage!\n", name, damage);
}

/************************************************************************/
void readRoom()
{

	errno_t err = fopen_s(&fp, DUNGEONS, "r");
	char buffer[100];
	if(err)
	{
		printf("Error opening file\n");
	}
	else 
	{
		printf("file contents\n--------------\n");
		while (!feof(fp))
		{
			fscanf_s(fp, "%s", buffer, 100);
			printf("%s\n", buffer);
			if(ferror(fp))
			{
				perror("Read error");
				break;
			}
		}
		fclose(fp);
	}
}

/************************************************************************/
void manipObject()
{
}

/************************************************************************/
void createChar()
{
	char buffer[25];
	int strength=0;
	errno_t err = fopen_s(&fp, CHARACTER, "a+");
	if(err)
	{
		printf("Error opening file\n");
	}
	else
	{
		printf("Please type a name for your characteer::> ");
		scanf_s("%s", &buffer, 25);
		fprintf_s(fp, "CharName = %s\n", buffer);
		printf("Select a race, [t]roll, [h]uman, [e]lf, [d]warf, [u]ndead. :: >");
		scanf_s("%s", &buffer, 25);
		fprintf_s(fp, "Race = %s\n", buffer);
		switch(buffer[0])
		{
		case 't':
			strength+=125;
			//	printf("%d\n", strength);
			break;
		case 'h': strength+=75;
			break;
		case 'e': strength+=90;
			break;
		case 'd': strength+=60;
			printf("%d\n", strength);
			break;
		case 'u': strength+=30;
		default: strength=0;
		}
		printf("Are you [m]ale or [f]emale? :: >");
		scanf_s("%s", &buffer, 25);
		fprintf_s(fp, "Sex = %s\n", buffer);
		switch(buffer[0])
		{
		case 'm':
			strength+=100;
			printf("%d\n", strength);
			break;  
		case 'f':
			strength+=75;
			printf("%d\n", strength);
		}
	}
	fclose(fp);
}

/************************************************************************/
void gameLoop()
{
	/* move the below way of looping to the combat funtion, use it to update the global? health var or something. Then just just keep checking each loop for death */
	int i=1;
	int health=10;
	while(health)
	{
	/*	for( i=0; i<10; ++i )
		{
			printf( "loop%d\n", i );
			system("cls")s;
		}*/
		fetchHealth( &health );
		printf("health: %d\n", health);

	}
	printf("You are dead\n");
}

/************************************************************************/
int readCharStats()
{
	int health=0;

	errno_t err = fopen_s( &fp, GAMESTATS, "r" );
	if( err )
	{
		printf( "Error opening file\n" );
		fopen_s( &fp, GAMESTATS, "w" );
		fclose( fp );
		readCharStats();
	}
	else 
	{
		fscanf_s( fp, "%s", health, 25 );
		printf( "%d\n", health );
		fclose( fp );
	}
	return health;
}

/************************************************************************/
void fetchHealth(int *h)
{
	--*h;
}

/************************************************************************/
void inittrade()
{
	int items1 = 0, items2 = 0;
	selectjunktotrade( items1, items2 );
	proctrade( items1, items2 );
}

/************************************************************************/
int selectjunktotrade( int junk1, int junk2 )
{
	return 0;
}

/************************************************************************/
void proctrade( int items1, int items2 )
{
	//FIX THIS CRAP TO READ THROUGH INVENTORY FILE AND FIND ITEM ID AND DELETE/UPDATE IT
	int inventory = 0;
	int i=0;
	errno_t err = fopen_s( &fp, INVENTORY, "w" );
	if ( err )
	{
		printf( "Unable to open file, retrying" );
		fclose( fp );
	}
	else
	{
		fprintf_s( fp, "Player 1\n% s \nPlayer 2\n %s", items1, items2 );
		for( i=0; i<25; ++i )
		{
		}
	}
}
/************************************************************************/